<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    <div class="container-div">
    	<div class="row">
	    	<div class="col-sm-12 search-collapse">
					<form id="file-form">
						<div class="select-list">
							<ul>
								<li>
									姓名：<input type="text" name="empNm" th:value="${baseInfo.empNm}" readonly="readonly"/>
								</li>
								<li>
									证件号码：<input type="text" name="empId" th:value="${baseInfo.empId}" readonly="readonly"/>
								</li>
								<li>
									<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								</li>
							</ul>
						</div>
					</form>
				</div>
			<div class="btn-group-sm hidden-xs" id="toolbar" role="group">
				<a class="btn btn-success" onclick="toAddHtml()" shiro:hasPermission="pm:fileInfo:add">
					<i class="fa fa-plus"></i> 添加
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="pm:fileInfo:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				 <!--<a class="btn btn-success" onclick="batchImportAttr()" >
			            <i class="fa fa-download"></i> 批量导入附件
			    </a>-->
				<a class="btn btn-warning" onclick="dlFilesForEmp()" shiro:hasPermission="pm:fileInfo:download">
		            <i class="fa fa-download"></i> 下载个人所有附件
		        </a>
			</div>
				
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
    </div>
    <div th:include="include :: footer"></div>
   <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" 
						aria-hidden="true">×
				</button>
				<h4 class="modal-title" id="myModalLabel">
					批量导入
				</h4>
			</div>
			<div class="modal-body">
				<input id="excelFile" name="excelFile" type="file" class="file" data-show-preview="false">
				<div class="text-center">
					 <button type="button" id="batchSubmit" class="btn btn-primary">批量导入</button>
			  		 <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				</div>
			  
			</div>
			
		</div> 
	</div>
   </div>
    <script th:inline="javascript">
        var removeFlag = [[${@permission.hasPermi('pm:fileInfo:remove')}]];
        var dlFileFlag = [[${@permission.hasPermi('pm:fileInfo:dlFile')}]];
        var prefix = ctx + "pm/fileInfo";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add/{id}",
                removeUrl: prefix + "/remove",
                modalName: "人员附件",
                queryParams: queryParams,
                search:false,
                showSearch:false,
                showColumns:false,
                showToggle:false,
                showExport:false,
                showRefresh:false,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'fileId', 
					title : '附件信息主键' 
				},
				{
					field : 'empId', 
					title : '证件号码' 
				},
				{
					field : 'empNm', 
					title : '姓名' 
				},
				{
					field : 'empAttType', 
					title : '附件类型' 
				},
				{
					field : 'empFileNm', 
					title : '文件名称' 
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-info btn-xs ' + dlFileFlag + '" href="#" onclick="dlFile(\'' + row.fileId + '\')"><i class="fa fa-download"></i>下载</a> ');
		            	return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        });
        
        function queryParams(params) {
			return {
				empId:          $("input[name='empId']").val(),
				pageSize:       params.limit,
    			pageNum:        params.offset/params.limit + 1,
    			searchValue:    params.search,
    			orderByColumn:  params.sort,
    			isAsc:          params.order
			};
		}
       
       function toAddHtml(){
    	  $.operate.add($("input[name='empId']").val());
       }
       
       // 下载附件ID为fileId的一个附件
       function dlFile(fileId){
			var data = [{name: 'fileId', value: fileId}];
			$.modal.loading("正在导出数据，请稍后...");
			$.post(prefix + "/dlFile", data, function(result) {
				if (result.code == web_status.SUCCESS) {
					alert("下载附件ID为"+fileId+"的附件成功。。。");
			        window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + false;
				} else {
					$.modal.alertError(result.msg);
				}
				$.modal.closeLoading();
			});
        }
       
   	   // 下载证件号码为empId的人员的所有附件
       function dlFilesForEmp(){
			var data = [{name: 'empId', value: $("input[name='empId']").val()}];
			$.modal.loading("正在导出数据，请稍后...");
			$.post(prefix + "/dlFilesForEmp", data, function(result) {
			    console.log(result)
				if (result.code == web_status.SUCCESS) {
					alert("下载用户所有的附件成功。。。");
			        window.location.href = ctx + "common/download?fileName=" + result.msg + "&delete=" + false;
				} else {
					$.modal.alertError(result.msg);
				}
				$.modal.closeLoading();
			});
        }
       
   	   
   	  
       function batchImportAttr(){
//      	 批量导入
  		$('#excelFile').fileinput('clear');
  		$('#myModal').modal('show');
       }	

          //初始化fileinput控件（第一次初始化）
    	function initFileInput(ctrlName, uploadUrl) { 
     		var control = $('#' + ctrlName);
     		control.fileinput({
     	   	 	     language: 'zh',
     		         uploadUrl: uploadUrl, //上传的地址
     		         showPreview: false,
     		         allowedFileExtensions : ['zip', 'ZIP'],//接收的文件后缀
     		         minFileCount: 1,                                        // 最小上传数量
     		         maxFileCount: 1,                                        // 最大上传数量
     		         showUpload: false, //是否显示上传按钮
     		         showCaption: true,//是否显示标题
     		         showRemove:false,                                       // 显示移除按钮
     		         uploadClass:"btn btn-success",                        // 上传按钮样式
     		         browseClass: "btn btn-primary", //按钮样式
     		         autoReplace:true,
     		         //enctype: 'multipart/form-data',
     		         previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
     	   })
        }
      initFileInput("excelFile", ctx + "pm/import/attrinfo");
      
  	$('#batchSubmit').on('click',function(){
      	$.modal.confirm("你确定要上传 ?", function() {
			  $('#excelFile').fileinput('upload');
		});
      })
      
  
  	 // uploadAsync设置为true并且已触发批量上传时被触发
  	// $("#excelFile").on("fileuploaded", function() {
  	// 		$('#myModal').modal('hide');
  	//        	$.modal.alertSuccess("批量导入成功");
  	// 		$.operate.saveSuccess(result);
  	//  });
  	// $("#excelFile").on('fileerror', function(event, data, msg) {
  	// 	$.modal.alertError("批量导入成功失败"+msg);
  	// });

	$("#excelFile").on("fileuploaded", function(event,data,previewid,index) {
		var result = data.response;
		if (result.code == '0') {
			$.modal.msgSuccess("批量导入成功");
			$.table.refresh();
			$('#myModal').modal('hide');
		} else {
			layer.alert(result.msg, {
				icon: 0,
				area:['60%','90%'],
				title: "系统提示",
			}, function (index) {
				layer.close(index);
				$('#myModal').modal('hide');
			});
		}
		$.modal.closeLoading();
	});
    </script>
</body>
</html>